package com.putao.album.widget.rounder;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.RelativeLayout;

/**
 * Created by yanglun on 15/5/21.
 */
public class RoundCornerRelativeLayout extends RelativeLayout {
    private float mCornerRadius;
    private Path mPath = new Path();
    private RectF mRect = new RectF();

    public RoundCornerRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        mCornerRadius = 20;
//        setWillNotDraw(false);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        mPath.reset();
//        mRect.set(0, 0, canvas.getWidth(), canvas.getHeight());
//        mPath.addRoundRect(mRect, mCornerRadius, mCornerRadius, Path.Direction.CCW);
//        mPath.close();
//        canvas.clipPath(mPath);
    }


    @Override
    protected void dispatchDraw(Canvas canvas) {
//        canvas.save();
//        canvas.clipRect(100,100,200,200);
//
//        super.dispatchDraw(canvas);
//         canvas.restore();

        canvas.save();
        mPath.reset();
        mRect.set(0, 0, canvas.getWidth(), canvas.getHeight());
        mPath.addRoundRect(mRect, mCornerRadius, mCornerRadius, Path.Direction.CCW);
        mPath.close();
        canvas.clipPath(mPath);
        super.dispatchDraw(canvas);
        canvas.restore();
    }

    public void setCornerRadius(int radius) {
        mCornerRadius = radius;
        invalidate();
    }
}
